#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtSerialPort>
#include "queue.h"
#include "qcustomplot.h"
#include "axistag.h"
#include "DebugView.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }

class QIntValidator;

QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    struct Settings {
        QString name;
        qint32 baudRate;
        QString stringBaudRate;
        QSerialPort::DataBits dataBits;
        QString stringDataBits;
        QSerialPort::Parity parity;
        QString stringParity;
        QSerialPort::StopBits stopBits;
        QString stringStopBits;
        QSerialPort::FlowControl flowControl;
        QString stringFlowControl;
    };

    Settings settings() const;

    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
Q_SIGNALS:
    void openDeviceSignal(bool open = true);

    void deviceStatusUpdateSignal(bool link = true);

public slots:
    void setDeviceStatusSlot(bool opened = false);

    void openSerialPortSlot(bool open);

    void readData();
    void writeData(const QByteArray &data);

    void timerSlot();
    void timerSerialSlot();


private slots:
    void on_pb_turn_off_clicked();

    void on_pb_turn_on_clicked();

    void on_pb_help_clicked();

    void on_pb_rst_clicked();

    void on_pb_pid_fusion_pid1_2_clicked();

    void on_pb_zero_clicked();

    void on_pb_zero_941b_clicked();

    void on_pb_motor_zero_clicked();

    void on_pb_par_list_clicked();

    void on_pb_par_save_clicked();

    void on_pb_par_set_clicked();

    void on_pb_par_load_clicked();

    void on_pb_time_clicked();

    void on_pb_motor_output_clicked();

    void on_pb_motor_xyz_output_clicked();

    void on_pb_dac_output_2_clicked();

    void on_pb_dac_output_3_clicked();

    void on_pb_pid_list_clicked();

    void on_pb_pid_pos_xy_clicked();

    void on_pb_pid_pos_z_clicked();

    void on_pb_pid_pos_individ_clicked();

    void on_pb_pid_spdfb1_x_clicked();

    void on_pb_pid_spdfb1_y_clicked();

    void on_pb_pid_spdfb1_z_clicked();

    void on_pb_pid_fusion_pid1_clicked();

    void on_pb_pid_fusion_pid1_3_clicked();

    void on_pb_pid_fusion_pid1_4_clicked();

    void on_pb_vf_est_in_3_clicked();

    void on_pb_vf_stop_clicked();

    void on_pb_vf_on_x_clicked();

    void on_pb_vf_result_clicked();

private:
    void fillPortsParameters();
    void fillPortsInfo();
    void updateSettings();

    void _saveState();
    void _restoreState();

    void historyAppend(QString &text);
private:
    Ui::MainWindow *ui;
    Settings m_currentSettings;
    QIntValidator *m_intValidator = nullptr;

    QSerialPort *m_serial = nullptr;
    QStringList m_valueList;//历史记录保存列表
    qsizetype m_valueIter = ~0;//记录一下回溯到哪

    QCustomPlot *mPlot;
    QPointer<QCPGraph> mGraph1;
    QPointer<QCPGraph> mGraph2;
    AxisTag *mTag1;
    AxisTag *mTag2;
    QTimer mDataTimer;
    QTimer mSerialDataTimer;
    QSerialPort *serial;//接收数据的串口
    bool m_IsOpened=false;

    //接收数据的环形队列
    RingBuffer *ringBuffer;
    DebugView *m_debugView = NULL;
};
#endif // MAINWINDOW_H
